document.addEventListener('DOMContentLoaded', function() {
    createBackground();
    createWatermark();
    
    // 监听窗口大小变化，重新生成水印和背景
    window.addEventListener('resize', function() {
        const oldWatermark = document.querySelector('.watermark');
        const oldBackground = document.querySelector('.background-layer');
        
        if (oldWatermark) {
            document.body.removeChild(oldWatermark);
        }
        if (oldBackground) {
            document.body.removeChild(oldBackground);
        }
        
        createBackground();
        createWatermark();
    });
    
function createBackground() {
    // 创建背景图层
    const background = document.createElement('div');
    background.className = 'background-layer';
    
    // 检查图片是否能加载
    const testImg = new Image();
    testImg.onload = function() {
        console.log('图片加载成功: bighouse.png');
    };
    testImg.onerror = function() {
        console.error('图片加载失败，请检查路径');
    };
    // 修改测试路径，与CSS保持一致
    testImg.src = './images/bighouse.png';
    
    // 将背景图层添加到body的第一个位置
    document.body.insertBefore(background, document.body.firstChild);
    
    console.log('背景图层已添加到页面');
}

    function createWatermark() {
        // 创建水印容器
        const watermark = document.createElement('div');
        watermark.className = 'watermark';
        
        // 创建水印内容容器
        const watermarkContent = document.createElement('div');
        watermarkContent.className = 'watermark-content';
        
        // 计算需要多少个水印项来填满屏幕
        const screenWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
        const screenHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
        
        // 估算每个水印项的大小（包括padding）
        const itemWidth = 180;
        const itemHeight = 80;
        
        // 计算对角线长度，确保旋转后能完全覆盖屏幕
        const diagonal = Math.sqrt(screenWidth * screenWidth + screenHeight * screenHeight);
        
        // 基于对角线长度计算行列数量，确保完全覆盖
        const columns = Math.ceil(diagonal / itemWidth) + 4;
        const rows = Math.ceil(diagonal / itemHeight) + 4;
        
        // 调整水印内容容器的位置，确保覆盖所有角落
        const offset = diagonal * 0.2;
        watermarkContent.style.top = `-${offset}px`;
        watermarkContent.style.left = `-${offset}px`;
        
        // 生成水印项
        for (let i = 0; i < rows; i++) {
            for (let j = 0; j < columns; j++) {
                const watermarkItem = document.createElement('div');
                watermarkItem.className = 'watermark-item';
                watermarkItem.textContent = '轩正公司管理系统';
                watermarkContent.appendChild(watermarkItem);
            }
        }
        
        // 将水印内容添加到水印容器
        watermark.appendChild(watermarkContent);
        
        // 将水印添加到body
        document.body.appendChild(watermark);
        
        console.log('水印已创建');
    }
});
